"""
EPSC_distances.py

obtain EPSCs waveforms as a function of the distance from the soma
ESPCs are collected in a stfio recording type to be analyzed in Stimfit

Last change:
Mon May  6 12:49:04 CEST 2013
"""

from neuron import h
import numpy as np

import stfio
from CA3.library import christoph2007


#-------------------------------------------------------------------------
# global variables for simulation
#-------------------------------------------------------------------------
tstop = 50 # in ms
v_init = -70 # in mV

#-------------------------------------------------------------------------
# create a CA3-15 morphology cell
# with a synapse at the soma
#
# Cm = 1 uF/cm^2
# Ra = 194 Ohms*cm
# Rm = 164 kOhms*cm^2
#-------------------------------------------------------------------------
mycell = christoph2007()
mysyn = h.synapse(0.5, sec = mycell.soma)
mysyn.tonset = 0.5 # ms
mysyn.tau0 = 0.2 # ms
mysyn.tau1 = 2.5 # ms
mysyn.gmax = 300e-6 # in uS

#-------------------------------------------------------------------------
# Voltage-clamp at the soma
# set soma as zero distance
# insert a synapse at the soma
#-------------------------------------------------------------------------
VClamp = h.SEClamp(0.5, sec= mycell.soma)
VClamp.rs = 0.1 # MegaOhms
VClamp.amp1 = v_init # see global variables 
VClamp.dur1 = tstop # see global variables

h.distance(0, 0.5, sec = mycell.soma) # set soma as origin

# see in synapse.mod
mysyn = h.synapse(0.5, sec = mycell.soma)
mysyn.tonset = 2.5 # ms
mysyn.tau0 = 0.2 # ms
mysyn.gmax = 300e-6 # in microSiemens 

#-------------------------------------------------------------------------
# Define simulation
#-------------------------------------------------------------------------
h.load_file('stdrun.hoc') # setup simulation
h.tstop = tstop # see global variables 
h.v_init = v_init # see global variables 
current = h.Vector()
current.record(VClamp._ref_i)

#-------------------------------------------------------------------------
# Iterate through all segments in mycell
#-------------------------------------------------------------------------
# idx, array, distance
distances = np.empty((mycell.nsec, 2 ))
epsc = list()
idx = 0
for sec in mycell.allsec:
    for seg in sec:
        mysyn.loc(seg.x, sec) # move syn to a new segment
        dist = h.distance(seg.x, sec = sec) # calculate distance from soma
        h.run()
        print('simulating epsc %d at %2.4f um from soma'%(idx, dist))
        epsc.append( np.array(current)*1000 ) # in pA
        distances[idx] = idx, dist
        idx +=1
        
#-------------------------------------------------------------------------
# Construct stfio Recording 
#-------------------------------------------------------------------------

np.savetxt('CA3_EPSCs_distances.out', distances, fmt='%d\t%f')
seclist = [stfio.Section(sweep) for sweep in epsc]
chlist = [stfio.Channel(seclist)]
chlist[0].yunits = 'pA'

rec = stfio.Recording(chlist)
rec.dt = h.dt
rec.xunits = 'ms'
rec.write('CA3_EPSCs_traces.h5') 
